/**@author Anshul : Release 4
 * This class is a replacement for the HomePage class we previously had. 
 * Code borrowed and modified with permission from Android Custom ListView Tutorial by S Janardhan 
 * http://www.androidpeople.com/android-custom-listview-tutorial-example/
 * Last Referenced: December 12th 2010. 
 * 
 * */
package com.UI;

import com.Model.StudentDBModel;
import android.app.Activity;
import android.os.Bundle;

/** Called when the activity is first created. */

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomePage extends Activity {
	// custom images are copyright and the property of Dennis Malovich who is a
	// member of the group for this project
	private static final String[] choose = { "Courses", "Schedule", "To-Do",
			"Grades", "Student Webmail", "Extra curricular Activities",
			"Google Search" };
	private static final int[] curr = { R.drawable.courses,
			R.drawable.schedule, R.drawable.todo, R.drawable.grades,
			R.drawable.webmail, R.drawable.extra, R.drawable.google };

	// class to extend BaseAdapter for a basic listviews
	private class EfficientAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);

		}

		public int getCount() {
			return choose.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.listview, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView
						.findViewById(R.id.TextView01);
				holder.text2 = (TextView) convertView
						.findViewById(R.id.TextView02);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final String startAct = choose[position];
			holder.text2.setText(startAct);
			holder.text.setBackgroundResource(curr[position]);

			// Onclick listener for each listview item
			convertView.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) { // --clickOnListItem
					if (startAct.equals("Courses")) {
						Toast.makeText(mInflater.getContext(), "View Courses",
								Toast.LENGTH_SHORT).show();
						Intent viewCourses = new Intent();
						viewCourses.setClassName("com.UI", "com.UI.CourseList");
						startActivity(viewCourses);
					}
					if (startAct.equals("Schedule")) {
						Toast.makeText(mInflater.getContext(),
								"Check Schedules", Toast.LENGTH_SHORT).show();
						Intent schedule = new Intent();
						schedule.setClassName("com.UI", "com.UI.Schedule");
						startActivity(schedule);
					}
					if (startAct.equals("To-Do")) {
						Toast.makeText(mInflater.getContext(), "To-Do's",
								Toast.LENGTH_SHORT).show();
						Intent viewtodo = new Intent();
						viewtodo.setClassName("com.UI", "com.UI.ToDoList");
						startActivity(viewtodo);
					}
					if (startAct.equals("Grades")) {
						Toast.makeText(mInflater.getContext(), "View Grades",
								Toast.LENGTH_SHORT).show();
						Intent viewGrades = new Intent();
						viewGrades.setClassName("com.UI", "com.UI.Grades");
						startActivity(viewGrades);
					}
					if (startAct.equals("Student Webmail")) {
						Toast.makeText(mInflater.getContext(), "Check Email",
								Toast.LENGTH_SHORT).show();
						// create a model instance
						StudentDBModel newWebmail = new StudentDBModel(
								HomePage.this);

						int numWebmails = newWebmail.getNumWebmails();
						// find out if a webmail has already been entered
						if (numWebmails > 0) {
							// a webmail has already been entered, so open it in
							// the browser
							Intent webmailGo = new Intent(HomePage.this,
									GoToStudentWebmail.class);
							startActivity(webmailGo);
						} else {
							// no webmail has been entered, so show the entry
							// form
							Intent webmailAdd = new Intent(HomePage.this,
									AddStudentWebmail.class);
							startActivity(webmailAdd);
						}
					}
					if (startAct.equals("Extra curricular Activities")) {
						Toast.makeText(mInflater.getContext(),
								"Do some new stuff", Toast.LENGTH_SHORT).show();
						Intent viewCurricular = new Intent();
						viewCurricular.setClassName("com.UI",
								"com.UI.ExtraCurricular");
						startActivity(viewCurricular);
					}
					if (startAct.equals("Google Search")) {
						Toast.makeText(mInflater.getContext(), "Google it!!",
								Toast.LENGTH_SHORT).show();
						Intent viewgoogle = new Intent();
						viewgoogle
								.setClassName("com.UI", "com.UI.GoogleSearch");
						startActivity(viewgoogle);
					}
				}
			});

			return convertView;
		}

		// ViewHolder class to show how the text will appear in each list view
		// item
		class ViewHolder {
			TextView text;
			TextView text2;
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		ListView l1 = (ListView) findViewById(R.id.ListView01);
		l1.setAdapter(new EfficientAdapter(this));
	}

	/*
	 * If the user wants to view the preferences. The following menu button
	 * click will take him/her to the preferences page
	 */

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.add_menu, menu);
		return true;
	}

	/**
	 * This is for the menu. If you want to add more items to it, just add them to the
	 * switch case and you are all done.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.preferences:
			Intent viewPreferences = new Intent();
			viewPreferences.setClassName("com.UI", "com.UI.Preferences");
			startActivity(viewPreferences);
			break;

		}
		return true;
	}

}
